package com.cssw.fyzb.wap.service;


import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.wap.model.vo.*;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Service
public class TaskService extends BaseService {

    @Autowired
    private MinaAppClient busiClient;

    /**
     * 任务中心
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxPage<com.cssw.fyzb.wap.model.vo.TaskVo>
     * @Param [userId, pageNumber, pageSize] 用户标识
     **/
    public AjaxPage<TaskVo> taskList(Long userId, int pageNumber, int pageSize) {
        Request request = new Request("ListTask");
        request.setParameter("userId", userId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        AjaxPage<TaskVo> taskVoAjaxPage = convertPage(busiClient.send(request), TaskVo.class);
        return taskVoAjaxPage;
    }

    /**
     * 我的任务
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxPage<com.cssw.fyzb.wap.model.vo.TaskAndInstVo>
     * @Param [userId, pageNumber, pageSize]
     **/
    public AjaxPage<TaskVo> myTaskList(Long userId, int pageNumber, int pageSize) {
        Request request = new Request("ListMyTask");
        request.setParameter("userId", userId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), TaskVo.class);
    }

    /**
     * 我的历史任务
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData<com.cssw.fyzb.wap.model.vo.TaskVo>
     * @Param [userId, pageNumber, pageSize] 用户标识
     **/
    public AjaxPage<TaskVo> oldTask(Long userId, int pageNumber, int pageSize) {
        Request request = new Request("OldTask");
        request.setParameter("userId", userId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), TaskVo.class);
    }

    /**
     * 领取任务
     *
     * @Param [userId, taskId] 任务标识
     **/
    public AjaxData<CollecTaskVo> collectTask(Long userId, String taskId, String pwd) {
        Request request = new Request("CollecTask");
        request.setParameter("userId", userId);
        request.setParameter("taskId", taskId);
        request.setParameter("pwd", pwd);
        return convertData(busiClient.send(request), CollecTaskVo.class);
    }

    /**
     * 今日任务进度
     *
     * @return
     * @Param
     **/
    private String taskProcess(Long userId) {
        Request request = new Request("TaskProcess");
        request.setParameter("userId", userId);
        AjaxData ajaxData = convertData(busiClient.send(request));
        Map<String, Object> map = (Map<String, Object>) ajaxData.getData();
        return map.get("progress").toString();
    }

    /**
     * 今日任务记录
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxPage<com.cssw.fyzb.wap.model.vo.TodayTaskRecordVo>
     * @Param [userId, pageNumber, pageSize]
     **/
    private AjaxPage<TodayTaskRecordVo> taskRecor(Long userId, int pageNumber, int pageSize) {
        Request request = new Request("MyTaskRecord");
        request.setParameter("userId", userId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), TodayTaskRecordVo.class);
    }

    /**
     * 今日进度平安果
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData<com.cssw.fyzb.wap.model.vo.TodayTaskProcessVo>
     * @Param [userId]
     **/
    public AjaxData<TodayTaskProcessVo> todayTask(Long userId, int pageNumber, int pageSize) {
        TodayTaskProcessVo todayTaskProcessVo = new TodayTaskProcessVo();
        AjaxPage ajaxPage = this.taskRecor(userId, pageNumber, pageSize);
        todayTaskProcessVo.setList(ajaxPage.getList());
        todayTaskProcessVo.setCount(ajaxPage.getCount());
        todayTaskProcessVo.setTotal(ajaxPage.getTotal());
        todayTaskProcessVo.setProgress(this.taskProcess(userId));
        AjaxData<TodayTaskProcessVo> ajaxData = new AjaxData();
        ajaxData.setData(todayTaskProcessVo);
        return ajaxData;
    }

    /**
     * 未结算的平安果
     *
     * @return com.ydn.appserver.Response
     * @Param [userId]
     **/
    private String unsettledValue(Long userId) {
        Request request = new Request("UnsettledValue");
        request.setParameter("userId", userId);
        AjaxData ajaxData = convertData(busiClient.send(request));
        Map<String, Object> map = (Map<String, Object>) ajaxData.getData();
        return map.get("sum").toString();
    }

    /**
     * 用户的所有平安果
     *
     * @return com.ydn.appserver.Response
     * @Param [userId]
     **/
    private String allValue(Long userId) {
        Request request = new Request("UserGBalanceFee");
        request.setParameter("userId", userId);
        request.setParameter("acctType", "G");
        AjaxData ajaxData = convertData(busiClient.send(request));
        Map<String, Object> map = (Map<String, Object>) ajaxData.getData();
        return map.get("balanceFee").toString();
    }

    /**
     * 一键结算
     *
     * @return
     * @Param
     **/
    public AjaxData settlement(Long userId) {
        Request request = new Request("Settlement");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request));
    }

    /**
     * 任务结算页面 平安果展示
     *
     * @return
     * @Param
     **/
    public AjaxData<ValueSettlementVo> valueExhibition(Long userId) {
        ValueSettlementVo vo = new ValueSettlementVo();
        vo.setBalanceFee(this.allValue(userId));
        vo.setSum(this.unsettledValue(userId));
        AjaxData ajaxData = new AjaxData();
        ajaxData.setData(vo);
        return ajaxData;
    }

    /**
     * 任务执行记录
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxPage<com.cssw.fyzb.wap.model.vo.TaskExecVo>
     * @Param [userId]
     **/
    public AjaxPage<TaskExecVo> listTaskExec(Long userId, Integer pageNumber, Integer pageSize, String status) {
        Request request = new Request("ListTaskExec");
        request.setParameter("userId", userId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        request.setParameter("status", status);
        return convertPage(busiClient.send(request), TaskExecVo.class);
    }

    /**
     * 每日任务
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData<com.cssw.fyzb.wap.model.vo.DailyTasksVo>
     * @Param [userId]
     **/
    public AjaxData<DailyTasksVo> dailyTasks(Long userId) {
        Request request = new Request("DailyTasks");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), DailyTasksVo.class);
    }

    /**
     * 看视频任务计数
     *
     * @param userId
     * @param advertId
     * @return
     */
    public AjaxData viewVideoCount(Long userId, String advertId, Integer timeLong) {
        Request request = new Request("ViewTaskVideoCount");
        request.setParameter("userId", userId);
        request.setParameter("advertId", advertId);
        request.setParameter("timeLong", timeLong);
        return convertData(busiClient.send(request), DailyTasksVo.class);
    }

    /**
     * 看视频任务时间计数
     *
     * @param userId
     * @param timeLong
     * @return
     */
    public AjaxData videoDayTask(Long userId, Integer timeLong) {
        Request request = new Request("VideoDayTask");
        request.setParameter("userId", userId);
        request.setParameter("timeLong", timeLong);
        return convertData(busiClient.send(request), DailyTasksVo.class);
    }

    /**
     * 判断用户是否设置支付密码
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData<com.cssw.fyzb.wap.model.vo.DailyTasksVo>
     * @Param [userId]
     **/
    public AjaxData<UserTransactionPwdVo> userTransactionPwd(Long userId) {
        Request request = new Request("UserTransactionPwd");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), UserTransactionPwdVo.class);
    }

    /**
     * 用户观看每日任务
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [userId, advert]
     **/
    public AjaxData viewDailyTask(Long userId, String advert) {
        Request request = new Request("ViewDailyTask");
        request.setParameter("userId", userId);
        request.setParameter("advert", advert);
        return convertData(busiClient.send(request));
    }

    /**
     * 每日任务
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData<com.cssw.fyzb.wap.model.vo.DailyTasksVo>
     * @Param [userId]
     **/
    public AjaxData<OnDayTaskVo> onDayTask(Long userId) {
        Request request = new Request("OnDayTask");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), OnDayTaskVo.class);
    }

    /**
     * 每日任务
     *
     * @Param [userId]
     **/
    public AjaxData<TaskDescVo> getTaskDescByType(Long userId, String taskType) {
        Request request = new Request("GetTaskDescByType");
        request.setParameter("userId", userId);
        request.setParameter("taskType", taskType);
        return convertData(busiClient.send(request), TaskDescVo.class);
    }
}
